--- title: "4、数的分解" created: 2025-11-28 tags: - 算法 --- # 4、数的分解 ## 题目 [数的分解](https://www.lanqiao.cn/paper/3842/problem/606/) ![[image-4f0e6736.png]] ## 思路分析 直接暴力的话 要算几十分钟 ```cpp #include using namespace std; bool check(string s){ if(count(s.begin(),s.end(),'2') || count(s.begin(),s.end(),'4')) return true; return false; } int main() { int a, b, c; int cnt = 0; for(a = 1; a < 2019; a++) { int i = a; string s = to_string(i); if(check(s)) continue; for(b = 1; b < 2019; b++) { int j = b; string s2 = to_string(j); if(check(s2)) continue; for(c = 1; c < 2019; c++) { int k = c; string s3 = to_string(k); if(check(s3)) continue; if(a!=b && a!=c && b!=c && a+b+c==2019){ cnt++; cout< using namespace std; bool check(int num){ string s=to_string(num); if(count(s.begin(),s.end(),'2') || count(s.begin(),s.end(),'4')) return false; return true; } int main() { vector valid; for(int i=1;i<2019;i++){ if(check(i)) valid.push_back(i); } int a, b, c; int cnt = 0; int n=valid.size(); for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { a=valid[i],b=valid[j]; c=2019-a-b; if(c>b && find(valid.begin(),valid.end(),c)!= valid.end()) cnt++; } } cout< using namespace std; bool check(int num){ string s=to_string(num); if(count(s.begin(),s.end(),'2') || count(s.begin(),s.end(),'4')) return false; return true; } int main() { // vector valid; // for(int i=1;i<2019;i++){ // if(check(i)) // valid.push_back(i); // } // int a, b, c; // int cnt = 0; // int n=valid.size(); // for(int i = 0; i < n; i++) { // for(int j = i+1; j < n; j++) { // a=valid[i],b=valid[j]; // c=2019-a-b; // if(c>b && find(valid.begin(),valid.end(),c)!= valid.end()) // cnt++; // } // } // cout<